﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Sockets;
using System.Net;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Threading;
using System.Diagnostics;
//using EncriptionLibrary;

namespace Goa
{
    public partial class Form1 : Form
    {

        [STAThread]
        static void Main(String[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        List<Patient> patienten = new List<Patient>();
        Dictionary<int, List<Session>> patientenDic = new Dictionary<int, List<Session>>();
        List<Session> sessions = new List<Session>();
        private int currentPatient = 0;
        private int heartrate;
        private int rpm;
        private double speed;
        private double distance;
        private string time;
        private int energy;
        private int pulse;
        private int power;
        private VoegPatientToe voegpattoe;
        private Database a;
        private BewerkPatient wijzigPatient;


        public Form1()
        {
            a = new Database();
            //Initializes the GUI
            InitializeComponent();
            initTabel();
        }


        public void initTabel() 
        {
            
            foreach (string s in a.getPatient()) {
                comboBoxPatient.Items.Add(s);
            }
            PatientTabel.AutoGenerateColumns = true;
            PatientTabel.ReadOnly = true;
        }

       
        public void RemoveOtherCharacters(string str)
        {
            string c = "";
            int select = 0;
            if ((str[0] >= '0' && str[0] <= '9')){
                for (int i = 0; i < str.Length; i++)
                {
                    if ((str[i] >= '0' && str[i] <= '9') || (str[i] == ':'))
                    {
                        c += str[i];
                    }
                    else
                    {
                        try
                        {
                            switch (select)
                            {
                                case 0:
                                    heartrate = Convert.ToInt32(c);
                                    break;
                                case 1:
                                    rpm = Convert.ToInt32(c);
                                    break;
                                case 2:
                                    speed = Convert.ToDouble(c) / 10;
                                    break;
                                case 3:
                                    distance = Convert.ToDouble(c) / 10;
                                    break;
                                case 4:
                                    power = Convert.ToInt32(c);
                                    break;
                                case 5:
                                    energy = Convert.ToInt32(c);
                                    break;
                                case 6:
                                    time = c;
                                    break;
                                case 7:
                                    pulse = Convert.ToInt32(c);
                                    break;
                            }
                        }
                        catch(FormatException)
                        {

                        }
                        c = "";
                        select++;
                    }
                }
            }
        }

       
        public void comboBoxPatient_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentPatient = comboBoxPatient.SelectedIndex;
            string stringetje = comboBoxPatient.Items[currentPatient].ToString();
            Tuple<string, string, string> tupeltje = a.getUser(stringetje);
            sessions = a.getSessionListByUserID(tupeltje.Item1);

            PatientTabel.DataSource = null;
            PatientTabel.DataSource = sessions;
            
        }

        private void PatientTabel_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
            if (!(e.RowIndex < 0))
            {
               
                PatientTabel.DataSource = null;
                PatientTabel.DataSource = sessions[e.RowIndex].ListOfValues;
                
            }
        }

        private void Btn_BackTabel_Click(object sender, EventArgs e)
        {

            PatientTabel.DataSource = null;
            PatientTabel.DataSource = sessions;

        }

        
        private void nieuwToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Voeg Patient toe!!
            voegpattoe = new VoegPatientToe(this, a);
            voegpattoe.ShowDialog();
           
        }

        public void krijgTerugPatient(string naam, string achternaam, string geslacht) {            
            comboBoxPatient.Items.Clear();
            foreach (string s in a.getPatient()) {
                comboBoxPatient.Items.Add(s);
            }

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            a.closeDB();
            Environment.Exit(0);
        }

        private void patientToolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            voegpattoe = new VoegPatientToe(this, a);
            voegpattoe.ShowDialog();
        }

        private void sessieToolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            //patienten[currentpatient].Session.Add(new Session(patienten[currentpatient].Session.Count));
            object o = PatientTabel.DataSource;
            PatientTabel.DataSource = null;
            PatientTabel.DataSource = o;
        }

        
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }

        private void patientgegevensToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            wijzigPatient = new BewerkPatient(this, a);
            wijzigPatient.ShowDialog();
        }
    }
}
